<template>
	<view class="bg-white">
		<uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" status-bar left-icon="left" left-text="返回"
			title="隐患登记" @clickLeft="back" />

		<!-- 基础表单校验 -->
		<uni-forms ref="valiForm" :rules="rules" :modelValue="model">
			<uni-forms-item label="隐患名称" required name="dangerName">
				<uni-easyinput v-model="model.dangerName" placeholder="请输入隐患名称" />
			</uni-forms-item>
			<uni-forms-item label="隐患描述" required name="dangerDesc">
				<uni-easyinput v-model="model.dangerDesc" placeholder="请输入隐患描述" />
			</uni-forms-item>
			<uni-forms-item label="装置" required name="riskDeviceId">
				<uni-data-select v-model="model.riskDeviceId" :localdata="riskDeviceList" @change="changeRiskDevice"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="单元" required name="riskUnitId" v-if="model.riskDeviceId != null">
				<uni-data-select v-model="model.riskUnitId" :localdata="riskUnitList"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="隐患来源" required name="dangerSrc">
				<uni-data-select v-model="model.dangerSrc" :localdata="dangerSrcList"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="隐患等级" required name="dangerLevel">
				<uni-data-checkbox v-model="model.dangerLevel" :localdata="dangerLevelList"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item label="治理类型" required name="dangerManageType">
				<uni-data-checkbox v-model="model.dangerManageType" :localdata="dangerManageTypeList" @change="changeDangerManageType"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item label="隐患类型" required name="hazardDangerType">
				<uni-data-select v-model="model.hazardDangerType" :localdata="hazardDangerTypeList"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="形成原因"  name="dangerReason">
				<uni-easyinput type="textarea" v-model="model.dangerReason" placeholder="请输入形成原因" />
			</uni-forms-item>
			<uni-forms-item label="控制措施"  name="controlMeasures">
				<uni-easyinput type="textarea" v-model="model.controlMeasures" placeholder="请输入控制措施" />
			</uni-forms-item>
			<uni-forms-item label="整改金额"  name="cost">
				<uni-easyinput v-model="model.cost" placeholder="请输入整改金额" />
			</uni-forms-item>
			<uni-forms-item label="隐患治理期限" name="dangerManageDeadline" v-if="model.dangerManageType == '1'">
				<uni-datetime-picker type="datetime" v-model="model.dangerManageDeadline" />
			</uni-forms-item>
			<uni-forms-item label="整改人" required name="liablePersonText">
				<!-- <uni-easyinput v-model="model.liablePerson" placeholder="请输入整改人" /> -->
				<uni-easyinput v-model="model.liablePersonText" placeholder="请输入整改人" disabled/>
				<button class="mini-btn" type="warn" size="mini" @click="selectUserList">查询</button>
			</uni-forms-item>
			<uni-forms-item label="图片" required name="img">
				<uni-section title="只选择图片" type="line">
					<view class="example-body">
						<!-- <uni-file-picker 
							limit="3"
							title="最多选择3张图片"
							@select="select"  /> -->
						<uni-file-picker @select="select" :autoUpload="false" :limit="3" title="最多选择3张图片"></uni-file-picker>
					</view>
				</uni-section>
			</uni-forms-item>
			
			
		</uni-forms>
		<button type="primary" loading="true" @click="submit('valiForm')">提交</button>
	</view>
</template>

<script>
	import api from '@/api/api.js'
	export default {
		data() {
			return {
				model: {},
				// 校验规则
				rules: {
					dangerName: {
						rules: [{
							required: true,
							errorMessage: '隐患名称不能为空'
						}]
					},
					dangerDesc: {
						rules: [{
							required: true,
							errorMessage: '隐患描述不能为空'
						}]
					},
					riskDeviceId: {
						rules: [{
							required: true,
							errorMessage: '装置不能为空'
						}]
					},
					riskUnitId: {
						rules: [{
							required: true,
							errorMessage: '单元不能为空'
						}]
					},
					dangerSrc: {
						rules: [{
							required: true,
							errorMessage: '隐患来源不能为空'
						}]
					},
					dangerLevel: {
						rules: [{
							required: true,
							errorMessage: '隐患等级不能为空'
						}]
					},
					dangerManageType: {
						rules: [{
							required: true,
							errorMessage: '治理类型不能为空'
						}]
					},
					hazardDangerType: {
						rules: [{
							required: true,
							errorMessage: '隐患类型不能为空'
						}]
					},
					liablePersonText: {
						rules: [{
							required: true,
							errorMessage: '整改人不能为空'
						}]
					},
				},
				dictCodeList: {
					dictCodeRiskDevice: "tb_base_risk_device,name,id",
					dictCodeRiskUnit: "tb_base_risk_unit,risk_unit_name,id",
					dictCodeDangerLevel: "dangerLevel",
					dictCodeDangerSrc: "dangerSrc",
					dictCodeDangerManageType: "dangerManageType",
					dictCodeHazardDangerType: "hazardDangerType",
				},
				url:{
					getUserListAll: "/sys/user/listAll",
					uploadUrl: "/sys/common/upload",
				},
				riskDeviceList: [], //装置list
				riskUnitList: [],//单元list
				dangerSrcList: [],//隐患来源
				dangerLevelList: [], //隐患等级list
				dangerManageTypeList: [],//治理类型
				hazardDangerTypeList: [],//隐患类型
			}
		},
		components: {},
		mounted() {
			this.initRiskDeviceList();
			this.initDangerLevelList();
			this.initDangerSrcList();
			this.initDangerManageTypeList();
			this.initHazardDangerTypeList();
		},
		methods: {
			initRiskDeviceList() {
				api.initDictData(this.dictCodeList.dictCodeRiskDevice).then(response =>{
					if(response.data.success){
						this.riskDeviceList = response.data.result
					}
				})
			},
			changeRiskDevice(value) {
				const dictCode = this.dictCodeList.dictCodeRiskUnit + ",device_id='"+ value +"'";
				api.initDictData(dictCode).then(response =>{
					if(response.data.success){
						this.riskUnitList = response.data.result
					}
				})
			},
			initDangerLevelList() {
				api.initDictData(this.dictCodeList.dictCodeDangerLevel).then(response =>{
					if(response.data.success){
						this.dangerLevelList = response.data.result
					}
				})
			},
			initDangerSrcList(){
				api.initDictData(this.dictCodeList.dictCodeDangerSrc).then(response =>{
					if(response.data.success){
						this.dangerSrcList = response.data.result
					}
					
				})
			},
			initDangerManageTypeList(){
				api.initDictData(this.dictCodeList.dictCodeDangerManageType).then(response =>{
					if(response.data.success){
						this.dangerManageTypeList = response.data.result
					}
				})
			},
			initHazardDangerTypeList(){
				api.initDictData(this.dictCodeList.dictCodeHazardDangerType).then(response =>{
					if(response.data.success){
						this.hazardDangerTypeList = response.data.result
					}
				})
			},
			selectUserList(val) {//引用用户
				this.nodeValue =val;
				this.$Router.push({
					name: 'selectUserListRadio',
				})
			},
			getValue(data) {
				this.model.liablePerson = data.username
				this.model.liablePersonText = data.realname
				this.$forceUpdate();//必须加强制刷新，否则不渲染
			},
			changeDangerManageType(selectRow){
				const currDateTime = new Date().getTime();
				this.model.registTime = currDateTime;//登记时间
				if(selectRow.detail.value == "0"){
					this.model.liablePerson = this.$store.getters.username;
					this.model.liablePersonText = this.$store.getters.realname;
					this.model.dangerManageDeadline = currDateTime;
					this.model.checkAcceptTime = currDateTime;
					this.model.checkAcceptPerson = this.$store.getters.username;
					this.model.dangerState = "9"
				}else if(selectRow.detail.value == "1"){
					this.model.liablePerson = null
					this.model.liablePersonText = null
					this.model.dangerManageDeadline = null
					this.model.checkAcceptTime = null
					this.model.checkAcceptPerson = null
					this.model.dangerState = "0"
				}
			},
			// 获取上传状态
			select(selectFile){
				selectFile.tempFilePaths.forEach(fileItem =>{
					this.$tip.loading();
					const fileParams = {filePath: fileItem,name: 'file'}
					api.uploadFiles(fileParams).then(response =>{
						this.$tip.loaded();
						if(response.data.success){
							if(this.model.img){
								this.model.img = this.model.img + "," + res.data.message;
							}else{
								this.model.img = response.data.message
							}
						}
					})
				})
			},
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			submit(ref) {
				this.$refs[ref].validate().then(res => {
					const baseDangerInvestigationReocrd = {}
					const tbBaseDangerInvestigationReocrdList = []
					if(this.model.dangerManageType == "0"){
						baseDangerInvestigationReocrd.governorTime = this.model.checkAcceptTime
						baseDangerInvestigationReocrd.governor = this.model.checkAcceptPerson
						baseDangerInvestigationReocrd.img = this.model.img
						baseDangerInvestigationReocrd.status = this.model.dangerState
					}else if(this.model.dangerManageType == "1"){
						baseDangerInvestigationReocrd.governorTime = this.model.dangerManageDeadline
						baseDangerInvestigationReocrd.governor = this.model.liablePerson
						baseDangerInvestigationReocrd.img = this.model.img
						baseDangerInvestigationReocrd.status = this.model.dangerState
					}
					tbBaseDangerInvestigationReocrdList.push(baseDangerInvestigationReocrd);
					this.model.registrant = this.$store.getters.username;
					this.model.tbBaseDangerInvestigationReocrdList = tbBaseDangerInvestigationReocrdList;
					api.addBaseDangerInvestigation(this.model).then(response =>{
						if(response.data.success){
							this.$tip.success("添加成功!")
							this.$Router.replace({name:'index'})
						}else{
							this.$tip.error("添加失败!");
						}
					})
				}).catch(err => {
					console.log('请填写必填项!');
				})
			},
		}
	} 
</script>

<style lang="scss">
	.bg-white {
		background-color: #fff;
	}

	.form-item {
		display: flex;
		align-items: left;
	}
	@mixin flex {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
	}
	@mixin height {
		/* #ifndef APP-NVUE */
		height: 100%;
		/* #endif */
		/* #ifdef APP-NVUE */
		flex: 1;
		/* #endif */
	}
	.popup-content {
		@include flex;
		align-items: center;
		justify-content: center;
		padding: 15px;
		width: 300px;
		height: 400px;
		background-color: #fff;
	}
	.popup-height {
		@include height;
		width: 200px;
	}
	.example-body {
		padding: 10px;
		padding-top: 0;
	}
</style>